package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.AttrItemBean;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19.
 */

public class ExchangeGoodAttrAdapter extends BaseAdapter {

    private List<List<AttrItemBean>> itemAttrList;
    private Handler handler;
    private Context context;
    private List<String> attrKeyList;

    public ExchangeGoodAttrAdapter(Context context, Handler handler,
                                   List<String> attrKeyList, List<List<AttrItemBean>> itemAttrList) {
        this.context = context;
        this.handler = handler;
        this.attrKeyList = attrKeyList;
        this.itemAttrList = itemAttrList;

    }

    @Override
    public int getCount() {
        if (attrKeyList != null && attrKeyList.size() > 0) {
            return attrKeyList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return attrKeyList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            view = View.inflate(context, R.layout.item_post_attr, null);
            vh = new ViewHolder();
            vh.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
            vh.gvList = (NoScrollGridView) view.findViewById(R.id.gvList);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        String attrKey = attrKeyList.get(i);
        vh.tvTitle.setText(attrKey);
        List<AttrItemBean> attrItemBeens = itemAttrList.get(i);//    [ ["十边形","att2929593f9c024ddd5"], ["六边形","att7364593f9960eb35b"] ]
        if (attrItemBeens.size() > 1) {
            int max = attrItemBeens.get(0).getName().length();
            int index = 0;
            List<AttrItemBean> itemBeanList = new ArrayList<>();
            for (int j = 0; j < attrItemBeens.size(); j++) {
                if (attrItemBeens.get(j).getName().length() > max) {
                    max = attrItemBeens.get(j).getName().length();
                    index = j;
                }
            }
            itemBeanList.add(attrItemBeens.get(index));
            for (int p = 0; p < attrItemBeens.size(); p++) {
                if (!attrItemBeens.get(p).getName().equals(attrItemBeens.get(index).getName())) {
                    itemBeanList.add(attrItemBeens.get(p));
                }
            }
            ExchangeAttrAdapter attrAdapter = new ExchangeAttrAdapter(context, itemBeanList);
            vh.gvList.setAdapter(attrAdapter);
            attrAdapter.notifyDataSetChanged();
            attrAdapter.setOnItemClickListener(new ExchangeAttrAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(AttrItemBean attrItemBean, int position) {
                    if (!"2".equals(attrItemBean.getStates())) {
                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("attrItemBean", attrItemBean);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    }
                }
            });
        } else {
            ExchangeAttrAdapter attrAdapter = new ExchangeAttrAdapter(context, attrItemBeens);
            vh.gvList.setAdapter(attrAdapter);
            attrAdapter.notifyDataSetChanged();
            attrAdapter.setOnItemClickListener(new ExchangeAttrAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(AttrItemBean attrItemBean, int position) {
                    if (!"2".equals(attrItemBean.getStates())) {
                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("attrItemBean", attrItemBean);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    }
                }
            });
        }
        return view;
    }

    private class ViewHolder {
        private TextView tvTitle;
        private NoScrollGridView gvList;
    }

}
